<?php

namespace app\admin\utils\controller;

use app\admin\app\model\ActionLog;
use app\common\controller\BaseController;
use plugin\user\app\model\User;
use support\Db;
use support\facade\Logger;
use support\Request;
use support\Response;

class IndexController extends BaseController
{
    /**
     * 重写仪表盘页面  查询自己关注的数据
     * @return void
     */

    /**
     * 不需要鉴权的方法
     * @var string[]
     */
    protected $noNeedAuth = ['dashboard'];

    /**
     * 仪表板
     * @param Request $request
     * @return Response
     * @throws \Throwable
     */
    public function dashboard(Request $request): Response
    {
        // 今日新增用户数
        $today_user_count = User::where('created_at', '>', date('Y-m-d') . ' 00:00:00')->count();
        // 7天内新增用户数
        $day7_user_count = User::where('created_at', '>', date('Y-m-d H:i:s', time() - 7 * 24 * 60 * 60))->count();
        // 30天内新增用户数
        $day30_user_count = User::where('created_at', '>', date('Y-m-d H:i:s', time() - 30 * 24 * 60 * 60))->count();
        // 总用户数
        $user_count = User::count();


        // 查询7天内的用户注册数
//        $day7_detail = [];
//        $now = time();
//        for ($i = 0; $i < 7; $i++) {
//            $date = date('Y-m-d', $now - 24 * 60 * 60 * $i);
//            $day7_detail[substr($date, 5)] = User::where('created_at', '>', "$date 00:00:00")
//                ->where('created_at', '<', "$date 23:59:59")->count();
//        }

        // 登陆日志
        $loginList = ActionLog::where(['url'=>'/app/admin/account/login'])->select('ip','create_at')
            ->orderBy('id','desc')->limit(5)->get();
        foreach ($loginList as $v){
            $v->ip_text.='';
        }

        $time = time();
        // 错误日志 近3天的查看
        $errLogList = [
            '今天'=>$this->getErrorLog(date('Ymd')),
            '昨天'=>$this->getErrorLog(date('Ymd',$time-86400)),
            '前天'=>$this->getErrorLog(date('Ymd',$time-86400*2)),
        ];

//        var_dump($errLogList);

        return view('utils/index/dashboard', [
            'today_user_count' => $today_user_count,
            'day7_user_count' => $day7_user_count,
            'day30_user_count' => $day30_user_count,
            'user_count' => $user_count,
//            'day7_detail' => array_reverse($day7_detail),

            'loginList'=>$loginList,
            'log_file'=>getLogSize(),
            'errLogList'=>$errLogList,

            'debug'=>config('app.debug'),
        ]);
    }

    public function echarts(){
        // 查询7天内的用户注册数
        $day7_detail = [];
        $now = time();
        for ($i = 0; $i < 15; $i++) {
            // 获取月初第一天
//            $date = date('Y-m', strtotime("-{$i} month"));
//            $lastDay = date('t', strtotime($date."-01"));
            $where = [
                // 指定时间内
//                ['created_at','between',],
                ['status','in',[3]],
            ];
            $date = date('Y-m-d', $now - 24 * 60 * 60 * $i);
            $value = User::where('created_at', '>', "$date 00:00:00")
                ->where('created_at', '<', "$date 23:59:59")->count();

            $day7_detail[] =[
                '当天日期'=>substr($date, 0),
                '注册人数'=>$value,
//                '订单数量'=>OrderList::where($where)
//                    ->where('created_at', '>', "$date 00:00:00")
//                    ->where('created_at', '<', "$date 23:59:59")
//                    ->sum('volume')
            ];
        }
        return json(['code'=>0,'data'=>$day7_detail]);
    }

    protected function getErrorLog($date){
        $reData = ['count'=>0,'500'=>0];
        $tableName = 'global_log_'.$date;
        // 如果表不存在 直接返回
        if(!Db::schema()->hasTable($tableName)){
            return $reData;
        }
        $reData['count'] = Db::table($tableName)->count();
        $reData['500'] = Db::table($tableName)->where(['response'=>500])->count();
        return $reData;
    }

    /**
     * 开启或者关闭调试模式
     * @return void
     */
    public function opendebug(Request $request){
        $status = $request->get('status',0);
        modifyEnv(['APP_DEBUG'=>$status]);
        return $this->json(0,$status==0?'开启调试成功':'关闭调试成功');
    }
}